package com.jd.jr.insights.dm.dim.controller;


import com.alibaba.fastjson.JSON;
import com.jd.jr.insights.dm.dim.domain.Business;
import com.jd.jr.insights.dm.dim.domain.DimTransform;

import com.jd.jr.insights.dm.dim.service.DimTransformService;

import com.jd.jr.insights.dm.dim.service.impl.DimTransformServiceImpl;
import com.jd.jr.insights.dm.dim.util.LOG;
import com.jd.jr.insights.dm.dim.util.easyui.datagrid.FliterRule;
import com.jd.jr.insights.dm.dim.util.easyui.datagrid.FliterRules;
import com.jfinal.core.Controller;
import com.jfinal.json.Json;
import sun.rmi.runtime.Log;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2017/7/15.
 */
public class DimTransformController extends Controller {


    DimTransformService dimTransformService = new DimTransformServiceImpl();

    public void index() {
        render("/dimTrans.html");
    }

    public void getAllTreeEnumList() {
        int dept_id = getParaToInt("dimId");
        LOG.INFO("dimId", dept_id + "");
        renderText("");
    }


    public void getAllList() {
        //LOG.INFO("getAllList()",dimTransformService.getDimTransformListJson());
        renderText(dimTransformService.getDimTransformListJson());
    }

    public void getAllListNm() {
//        LOG.INFO("getAllList()", dimTransformService.getDimTransformListJson());
        renderText(dimTransformService.getDimTransformListJson());
    }

    public void getAllListInPage() {

        String filterRules=getPara("filterRules");


        int pageIndex = getParaToInt("page") == null ? 1 : getParaToInt("page");   //第几页的数据
        int pageSize = getParaToInt("rows") == null ? 20 : getParaToInt("rows");  //每页多少条数据
//        LOG.INFO("getAllListInPage()--page", pageIndex + "");
//        LOG.INFO("getAllListInPage()--rows", pageSize + "");
        //int pageNum = getParaToInt("pageNum")==null?1:getParaToInt("pageNum");
        int pageNum = getParaToInt("page") == null ? 1 : pageIndex;
        if(filterRules==null) {
            renderText(dimTransformService.getDimTransformListInPage(pageNum));
        }else{
            DimTransform dimTransform = new DimTransform();
            List<FliterRule> fliterRules= JSON.parseArray(filterRules, FliterRule.class);
            Iterator<FliterRule> inter=fliterRules.iterator();
            while(inter.hasNext()) {
                FliterRule rule=inter.next();
                if(rule.getField().equals("old_table_nm")&&!rule.getValue().equals("")){
                    dimTransform.setOld_table_nm(rule.getValue());
                }
                if(rule.getField().equals("new_column_nm")&&!rule.getValue().equals("")){
                    dimTransform.setNew_column_nm(rule.getValue());
                }
                if(rule.getField().equals("biz_id")&&!rule.getValue().equals("")){
                    Business biz=new Business();
                    biz.setBiz_id(Integer.parseInt(rule.getValue()));
                    dimTransform.setBiz(biz);
                }
            }
            renderText(dimTransformService.getDimTransformListInPage(pageNum,dimTransform));
        }
    }


    public void save() {

        Enumeration e = getAttrNames();
        while (e.hasMoreElements()) {
            Object o = e.nextElement();
            LOG.INFO("save()",o.toString());
        }

        Integer id = getParaToInt("id");
        String old_table_nm = getPara("old_table_nm");
        String old_column_nm1 = getPara("old_column_nm1");
        String old_key_id1 = getPara("old_key_id1");
        String old_key_nm1 = getPara("old_key_nm1");
        String new_table_nm = getPara("new_table_nm");
        String new_column_nm = getPara("new_column_nm");
        String new_key_id = getPara("new_key_id");
        String new_key_nm = getPara("new_key_nm");
        Integer biz_id = getParaToInt("biz_id");





        Business bussBusiness = new Business();
        bussBusiness.setBiz_id(biz_id);
        DimTransform dimTransform = new DimTransform(id, old_table_nm, old_column_nm1, old_key_id1, old_key_nm1, new_table_nm, new_column_nm, new_key_id, new_key_nm, bussBusiness);


        if (id == null) {
            renderText(dimTransformService.save(dimTransform));
//            LOG.INFO("保存方法");
        } else {
            renderText(dimTransformService.update(dimTransform));
//            LOG.INFO("更新方法");
        }

/*        Integer parent_id=getParaToInt("parent_id");
        String code=getPara("code");
        String desc=getPara("desc");
        String name=getPara("value");
        Integer level=getParaToInt("level");

        DimTransform dimTransform=new DimTransform();

        dimTransform.setCode(code);
        dimTransform.setValue(name);
        dimTransform.setParent_id(parent_id);
        dimTransform.setDesc(desc);
        dimTransform.setLevel(level);
        renderText(dimTransformService.save(dept));*/
    }

    public void update() {
        Enumeration e = getAttrNames();
        while (e.hasMoreElements()) {
            Object o = e.nextElement();
            LOG.INFO("update()", o.toString());
        }
/*        Integer id=getParaToInt("id");
        String code=getPara("code");
        String name=getPara("value");
        String desc=getPara("desc");


        Department dept=new Department();
        dept.setId(id);
        dept.setCode(code);
        dept.setValue(name);
        dept.setDesc(desc);
        renderText(dimTransformService.update(dept));*/

    }

    public void deleteByIds() {
        Enumeration e = getAttrNames();
        while (e.hasMoreElements()) {
            Object o = e.nextElement();
            LOG.INFO("deleteById()", o.toString());
        }
        Integer[] ids = getParaValuesToInt("ids[]");
        LOG.INFO("ids:" + ids);
        renderText(dimTransformService.deleteByIds(ids));

    }


    public void getByDimDefId() {
        Enumeration e = getAttrNames();
        while (e.hasMoreElements()) {
            Object o = e.nextElement();
            LOG.INFO("deleteById()", o.toString());
        }
        renderText("");
    }

    public void generateLvlTableData() {

    }

    public void saveTreeToLvl() {

    }

    public void getOldTableNameList() {
        renderText(dimTransformService.getOldTableNameList());
    }

    public void getNewTableNameList() {
        renderText(dimTransformService.getNewTableNameList());

    }

    public void getNewColumnNameList() {
        renderText(dimTransformService.getNewColumnNameList());

    }

    public void getOldKeyIdList() {
        renderText(dimTransformService.getOldKeyIdList());
    }

    public void getOldKeyNameList() {
        renderText(dimTransformService.getOldKeyNameList());

    }
    public void getBizList() {
        renderText(dimTransformService.getBizList());

    }

}
